import config from '@/utils/request/config';

/**
 * 检查请求体大小并在开发环境下打印警告
 * @param body - 请求体
 */
export const checkBodySize = (body: any): void => {
  if (import.meta.env.DEV && body) {
    try {
      const bodyStr = JSON.stringify(body);
      if (bodyStr.length > config.MAX_BODY_SIZE) {
        console.warn(
          `Request body is too large (${bodyStr.length} bytes). ` +
            `Consider optimizing your payload. Max allowed: ${config.MAX_BODY_SIZE} bytes`
        );
      }
    } catch (e) {
      console.error('Failed to check body size:', e);
    }
  }
};

/**
 * 创建AbortController
 */
export const createAbortController = (): { controller: AbortController; signal: AbortSignal } => {
  const controller = new AbortController();
  return { controller, signal: controller.signal };
};
